using HCore.Unity.Core;
using UnityEngine;

namespace HCore.Tests.Runtime.ShootingEditor2D.Scripts.ViewController.GamePlay
{
    public class CameraController : MonoBehaviour
    {
        private const float _MAX_X = 5;
        private const float _MAX_Y = 5;
        private const float _MIN_X = -5;
        private const float _MIN_Y = -5;

        // private Vector3 mTargetPos = new();

        private void LateUpdate()
        {
            var playerGo = GameUtils.MainPlayerGameObject;
            if (playerGo == null) return;

            // 面向不同方向，摄像机也需要调整，跟着转向
            var isRight = Mathf.Sign(playerGo.transform.localScale.x); // 获取到缩放值的正负符号

            // 摄像机跟随(要做平滑处理)
            var transform1 = transform;
            var cameraPos = transform1.position;
            var playerPos = playerGo.transform.position;
            var targetPos = new Vector3(playerPos.x + 3 * isRight, playerPos.y + 2, cameraPos.z);

            // 平滑处理
            cameraPos = Vector3.Lerp(cameraPos, targetPos, 1 * Time.deltaTime);

            // 锁定区域
            cameraPos = new Vector3(
                Mathf.Clamp(cameraPos.x, _MIN_X, _MAX_X),
                Mathf.Clamp(cameraPos.y, _MIN_Y, _MAX_Y),
                cameraPos.z
            );

            transform.SetLocalPositionAndRotation(cameraPos, Quaternion.identity);
        }
    }
}